package leetcode_2401_2500;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_2492 {
    public static void main(String[] args) {

    }

    private static int minScore(int n, int[][] roads) {
        int[] p = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            p[i] = i;
        }
        for (int[] road : roads) {
            union(p, road[0], road[1]);
        }
        int root = find(p, 1);
        Set<Integer> set = new HashSet<>();
        for (int i = 1; i <= n; i++) {
            if (find(p, i) == root) {
                set.add(i);
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int[] road : roads) {
            if (set.contains(road[0]) || set.contains(road[1])) {
                ans = Math.min(ans, road[2]);
            }
        }
        return ans;
    }

    //    private static int find(int[] p, int x){
//        if (x == p[x])
//            return x;
//        return find(p, p[x]);
//    }
    private static int find(int[] p, int x) {
        if (x != p[x]){
            p[x] = find(p, p[x]);
        }
        return p[x];
    }

    private static void union(int[] p, int x, int y) {
        int rx = find(p, x);
        int ry = find(p, y);
        p[rx] = ry;
    }
}
